package appapi.utils;

import org.apache.poi.ss.formula.functions.T;

import javax.swing.text.html.parser.Entity;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author djq
 * @date 2023/6/25 15:04
 * @description:
 */
/**
 * 基于反射，将obj转为map
 */
public class List2MapUtil {
    /**
     * Pojo -> Map<String, Object>
     * @param obj
     * @return
     * @throws Exception
     */
    public static Map<String,Object> object2Map(Object obj) throws Exception{
        Map<String,Object> map = new HashMap<String, Object>();
        Field[] fields = obj.getClass().getDeclaredFields();
        for(Field field:fields){
            field.setAccessible(true);
            map.put(field.getName(), field.get(obj));
        }
        return map;
    }

    /**
     * 将一个类查询方式加入map（属性值为int型时，0时不加入，
     * 属性值为String型或Long时为null和“”不加入）
     *注：需要转换的必须是对象，即有属性
     */
    public static Map<String, Object> setConditionMap(Object obj){
        Map<String, Object> map = new HashMap<>();
        if(obj==null){
            return null;
        }
        Field[] fields = obj.getClass().getDeclaredFields();//获取类的各个属性值
        for(Field field : fields){
            String fieldName =  field.getName();//获取类的属性名称
            if(getValueByFieldName(fieldName,obj)!=null)//获取类的属性名称对应的值
            {
                map.put(fieldName,  getValueByFieldName(fieldName,obj));
            }
        }
        return map;
    }

    /**
     * 根据属性名获取该类此属性的值
     * @param fieldName
     * @param object
     * @return
     */
    private static Object getValueByFieldName(String fieldName, Object object){
        String firstLetter=fieldName.substring(0,1).toUpperCase();
        String getter = "get"+firstLetter+fieldName.substring(1);
        try {
            Method method = object.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(object, new Object[] {});
            return value;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * List<T> --> List<Map<String, Object>>
     * @param objectList
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<Map<String, Object>> objectList2ListMap(List<T> objectList) throws Exception {
        ArrayList<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        for (T t : objectList) {
            resultList.add(object2Map(t));
        }
        return resultList;
    }

    /**
     * List<T> --> Map<String, List<Object>>
     * @param objectList
     * @param keyName
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> Map<String, List<Object>> objectList2MapList(List<T> objectList, String[] keyName) throws Exception{
        Map<String, List<Object>> resultMap = new HashMap<>();
        for(int i = 0; i < keyName.length; i++){
            List<Object> arrayList = new ArrayList<>();
            for (T t: objectList){// List有序，所以对每个对象依次变为map,然后得到对应的值，存入arrayList
                arrayList.add(object2Map(t).get(keyName[i]));
            }
            resultMap.put(keyName[i], arrayList);//将keyName和对应List集合存入resultMap
        }
        return resultMap;
    }
}
